package com.edgardeng.publish_subscribe.topic;

import com.edgardeng.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.util.Date;

/**
 * RabbitMQ 测试 Publish/Subscribe  - 发布者发送消息
 *
 */
public class Sender {
    public static void main(String[] args) throws Exception{
        String exchange = ConnectionUtil.EXCHANGE_NAME_TOPIC;
        String type = ConnectionUtil.EXCHANGE_TYPE_TOPIC;
        System.out.println("[Sender] exchange: " + exchange + "    type: " + type);

        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(exchange, type); // 声明交换机

        String[] routes = {
                "orange.animal.cat",
                "orange.animal",
                "orange.juice",
                "orange.at.here",
                "i.like.apple" ,
                "orange", "dog",
                "orange.cat", "dog.like.apple", "dog.like2.apple",
                "dog.apple"
        };
        for (String routeKey : routes) {
            String message = "I'm sending Publish/Subscribe message in route: "+ routeKey + " at " + new Date().toString();
            channel.basicPublish(exchange, routeKey,null, message.getBytes()); // 发送消息
            System.out.println("[Sender] " + message);
        }

        channel.close();
        connection.close();
    }


}
